package birdsVSZombiesServer;

import java.util.ArrayList;

import shared.BVZConstants;
import shared.BVZServerToClientsEvents;
import fisica.FBody;
import fisica.FContactResult;
import birdsVSZombiesComponents.*;

public class BirdsVSZombiesCollision {
	
	public BirdsVSZombiesServerThread server;
	public FContactResult collision;
	public AbstractBox2DShape[] collisionItems = new AbstractBox2DShape[2];
	public AbstractBox2DShape collisionItem1;
	public AbstractBox2DShape collisionItem2;
	
	public Object[] collisionObjects = new Object[2];
	
	public FBody[] itemBodies = new FBody[2];
	public FBody item1Body;
	public FBody item2Body;
	
	public ArrayList[] itemGroups = new ArrayList[2];
	public ArrayList item1Group;
	public ArrayList item2Group;
	
	//	The id in the arraylist.
	public int[] itemIDs = new int[2];
	public int item1ID;
	public int item2ID;
	
	public String[] itemTypes = new String[2];
	
	public int[] itemMaxCollisionNum = new int[2];
	
//	public BVZServerToClientsEvents colEvent;
	
	public BirdsVSZombiesCollision(FContactResult _result, BirdsVSZombiesServerThread _server){
		this.collision = _result;
		this.server = _server;
		
		getCollisionItems();
//		this.colEvent = new BVZServerToClientsEvents(BVZConstants.COLLISIONEVENT, this);
//		System.out.println("col!!!");
	}
	
	//	Get which is which of the two items which are colliding
	public void getCollisionItems(){
		switch (this.collision.getBody1().getBox2DBody().bodyMaster) {
			case BVZConstants.BIRDSSHOOTER:
				
				//	Get the bird which is colliding.
				for(int i=0; i<this.server.birds.size(); i++){
					if(this.server.birds.get(i).baseCircle == this.collision.getBody1()){
						this.itemIDs[0] = i;
						this.itemGroups[0] = this.server.birds;
						this.collisionItems[0] = this.server.birds.get(i);
//						this.collisionObjects[0] = this.server.birds.get(i);
						this.itemBodies[0] = this.server.birds.get(i).baseCircle;
						this.itemTypes[0] = this.server.birds.get(i).baseType;
						this.itemMaxCollisionNum[0] = this.server.birds.get(i).maxCollisionNum;
						break;
					}
				}
				
				if(this.collision.getBody2().getClass().getName() == "fisica.FBox"){
					for(int i=0; i<this.server.blocks.size(); i++){
						if(this.server.blocks.get(i).baseBox == this.collision.getBody2()){
							this.itemIDs[1] = i;
							this.itemGroups[1] = this.server.blocks;
							this.collisionItems[1] = this.server.blocks.get(i);
//							this.collisionObjects[1] = this.server.blocks.get(i);
							this.itemBodies[1] = this.server.blocks.get(i).baseBox;
							this.itemTypes[1] = this.server.blocks.get(i).baseType;
							this.itemMaxCollisionNum[1] = this.server.blocks.get(i).maxCollisionNum;
						}
					}
				}
				else if(this.collision.getBody2().getClass().getName() == "fisica.FCircle"){
					for(int i=0; i<this.server.zombies.size(); i++){
						if(this.server.zombies.get(i).baseCircle == this.collision.getBody2()){
							this.itemIDs[1] = i;
							this.itemGroups[1] = this.server.zombies;
							this.collisionItems[1] = this.server.zombies.get(i);
//							this.collisionObjects[1] = this.server.zombies.get(i);
							this.itemBodies[1] = this.server.zombies.get(i).baseCircle;
							this.itemTypes[1] = this.server.zombies.get(i).baseType;
							this.itemMaxCollisionNum[1] = this.server.zombies.get(i).maxCollisionNum;
						}
					}
				}
				
				break;
			
			case BVZConstants.ZOMBIESDEFENDER:
				
				if(this.collision.getBody1().getClass().getName() == "fisica.FBox"){
					for(int i=0; i<this.server.blocks.size(); i++){
						if(this.server.blocks.get(i).baseBox == this.collision.getBody1()){
							this.itemIDs[0] = i;
							this.itemGroups[0] = this.server.blocks;
							this.collisionItems[0] = this.server.blocks.get(i);
//							this.collisionObjects[0] = this.server.blocks.get(i);
							this.itemBodies[0] = this.server.blocks.get(i).baseBox;
							this.itemTypes[0] = this.server.blocks.get(i).baseType;
							this.itemMaxCollisionNum[0] = this.server.blocks.get(i).maxCollisionNum;
						}
					}
				}
				else if(this.collision.getBody1().getClass().getName() == "fisica.FCircle"){
					for(int i=0; i<this.server.zombies.size(); i++){
						if(this.server.zombies.get(i).baseCircle == this.collision.getBody1()){
							this.itemIDs[0] = i;
							this.itemGroups[0] = this.server.zombies;
							this.collisionItems[0] = this.server.zombies.get(i);
//							this.collisionObjects[0] = this.server.zombies.get(i);
							this.itemBodies[0] = this.server.zombies.get(i).baseCircle;
							this.itemTypes[0] = this.server.zombies.get(i).baseType;
							this.itemMaxCollisionNum[0] = this.server.zombies.get(i).maxCollisionNum;
						}
					}
				}
				
//				Get the bird which is colliding.
				for(int i=0; i<this.server.birds.size(); i++){
					if(this.server.birds.get(i).baseCircle == this.collision.getBody2()){
						this.itemIDs[1] = i;
						this.itemGroups[1] = this.server.birds;
						this.collisionItems[1] = this.server.birds.get(i);
//						this.collisionObjects[1] = this.server.birds.get(i);
						this.itemBodies[1] = this.server.birds.get(i).baseCircle;
						this.itemTypes[1] = this.server.birds.get(i).baseType;
						this.itemMaxCollisionNum[1] = this.server.birds.get(i).maxCollisionNum;
						break;
					}
				}
				
				break;
				
			default:
				break;
		}
	}
	
	public void setItemsAttributes(int id, int i){
		
	}
	
	/*********************************************************
	 *	Update the items related to the collision. Change the current collision number,
	 *	remove the items if they hit the collision limitations.
	 *	And the clients will update the texture for the items.
	 *********************************************************/
	public void updateColItems(){
		for(int i=0; i<2; i++){
			this.collisionItems[i].currentCollisionNum++;
			
//			System.out.println(this.collisionItems[i].currentCollisionNum);
			
			if(this.collisionItems[i].currentCollisionNum >= this.itemMaxCollisionNum[i]){
				removeColItemFromTheWorld(i);
			}
		}
//		
//		if(this.collisionObjects[0] instanceof Birds){
//			Birds colBird = (Birds) this.collisionObjects[0];
//		}
		
	}
	
	public void removeAllColItemsFromTheWorld(){
//		this.server.world.remove(this.collisionItem1.g)
		for(int i=0; i<2; i++){
			this.server.world.remove(this.itemBodies[i]);
			this.itemGroups[i].remove(this.itemIDs[i]);
		}
	}
	
	public void removeColItemFromTheWorld(int i){
		this.server.world.remove(this.itemBodies[i]);
		this.itemGroups[i].remove(this.itemIDs[i]);
	}
	
	public void sendColEventToClients(){
//		this.server.bvzServer.sendToAllTCP(colEvent);
	}
}






